package com.m2bshop.shop.service.WalletService;

import com.m2bshop.shop.dao.WalletMapper;
import com.m2bshop.shop.pojo.Wallet;
import com.m2bshop.shop.service.BankCardService.BankCardService;
import com.m2bshop.shop.service.TransactionService.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class WalletServiceImpl implements WalletService{

    @Autowired
    WalletMapper walletMapper;
    @Autowired
    BankCardService bankCardService;
    @Autowired
    TransactionService transactionService;


    //充值和提现，type为0=充值，为1=提现，number是充值或提现的金额
    @Override
    public String UpdateBalanceByBank(Wallet wallet,int type,double number,int bankid) {
        String info=null;
        int i=1;
        if(type==0)// 充值
        {
            System.out.println("充值");
            System.out.println(wallet.getUserid());
            System.out.println(wallet.getBalance());
            i=bankCardService.UpdateBalance(bankid,1,number);//银行卡充值
            if(i==0)
            {
                info="充值失败，银行卡余额不足";
            }
            else {
                info="充值成功";
                wallet.setBalance(walletMapper
                        .getInfo(wallet.getUserid())
                        .getBalance() + number);
                transactionService.insertNewTransaction(number,"充值",wallet.getUserid());
            }
            System.out.println(wallet.getBalance());
        }
        else if(type==1) {//提现
            double internalBan=walletMapper
                    .getInfo(wallet.getUserid())
                    .getBalance()-number;
            System.out.println("提现");
            if(internalBan<0) {
                info = "提现失败,钱包余额不足";
            }
            else {
                info="提现成功";
                wallet.setBalance(internalBan);
                bankCardService.UpdateBalance(bankid,0,number);
                transactionService.insertNewTransaction(number,"提现",wallet.getUserid());
            }
        }
        walletMapper.Update(wallet);
        return info;
    }

    //支付接口
    @Override
    public String UpdateBalanceByExpense(int userid,double number) {
        String info=null;
        Wallet wallet=walletMapper.getInfo(userid);
        if(wallet.getBalance()-number<0)
        {
            info="余额不足，请先充值";
        }
        else {
            wallet.setBalance(wallet.getBalance()-number);
            walletMapper.Update(wallet);
            transactionService.insertNewTransaction(number,"支出",userid);
            info="支付成功";
        }
        return info;
    }

    public String UpdateWallet(Wallet wallet){//修改钱包基本信息
        walletMapper.Update(wallet);
        return "修改信息成功";
    }

    @Override
    public int getUserWallet(int userid) {
        return walletMapper.getUserInfo(userid);
    }

    @Override
    public String InsertNewWallet(Wallet wallet) {
        walletMapper.UpdateWallet(wallet.getUserid());
        wallet.setBalance(0);
        walletMapper.Insert(wallet);
        return "新增成功";
    }
    @Override
    public Wallet getWalletInfo(int id) {
        Wallet wallet=walletMapper.getInfo(id);
        return wallet;
    }
}
